package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.dto.ServiceDetailDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.entity.TServerDetail;
import com.woniu.car.entity.TServerOrder;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.mapper.TServerDetailMapper;
import com.woniu.car.service.TServerDetailService;
import com.woniu.car.service.TServerOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author *********
 * @since 2021-06-21
 */
@Service
public class TServerDetailServiceImpl extends ServiceImpl<TServerDetailMapper, TServerDetail> implements TServerDetailService {

    @Resource
    private TServerOrderService tServerOrderService;

    /**
    *@Author 李磊
    *@Description 新增订单详情
    *@Param [orderDetailList]
    *@return void
    **/
//    @Override
//    public void insertDetail(List<OrderDetailParam> orderDetailList,Integer orderId){
//
//        for (OrderDetailParam orderDetailParam : orderDetailList) {
//
//            double total = 0;
//
//            TServerDetail detail = new TServerDetail();
//            BeanUtils.copyProperties(orderDetailParam,detail);
//
//            Double price = orderDetailParam.getPrice();
//            Integer number = orderDetailParam.getNumber();
//
//            if (null != orderDetailParam.getDiscountMoney()) {
//                total = price * number * orderDetailParam.getDiscountMoney();
//            } else {
//                total = price * number - orderDetailParam.getFullMoney();
//            }
//
//            //生成订单详情编号
//            String code = UUID.randomUUID().toString();
//            detail.setDetailNumber(code);
//            detail.setTotal(total);
//            detail.setOrderId(orderId);
//
//            //新增订单详情
//            this.save(detail);
//
//        }
//    }

    @Override
    @Transactional
    public List<ServiceDetailDto> updateAfterPay(Integer orderId) throws Exception {

        if (null == orderId || orderId <= 0) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TServerOrder byId1 = tServerOrderService.getById(orderId);

        if (null == byId1) {
            throw new ServiceSException(CommonConstant.RESULTNULL);
        }

        if (byId1.getStatus().equals(CommonConstant.ONE)) {
            throw new ParamException(CommonConstant.ORDERWRONG);
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id",orderId);
        List<TServerDetail> list = this.list(wrapper);

        if (null == list || list.size() <= 0) {
            throw new ServiceSException(CommonConstant.RESULTNULL);
        }

        TServerOrder order = new TServerOrder();
        order.setOrderId(orderId);
        order.setStatus(CommonConstant.ONE);
        order.setPayStatus(CommonConstant.ONE);
        tServerOrderService.updateById(order);

        List<ServiceDetailDto> serviceDetailDtos = new ArrayList<>();

        for (TServerDetail tServerDetail : list) {

            String qrCode = UUID.randomUUID().toString();
            Integer detailId = tServerDetail.getDetailId();
            TServerDetail detail = new TServerDetail();
            detail.setDetailId(detailId);
            detail.setQrCode(qrCode);

            this.updateById(detail);
            TServerDetail byId = this.getById(detailId);

            ServiceDetailDto serviceDetailDto = new ServiceDetailDto();
            BeanUtils.copyProperties(byId,serviceDetailDto);
            serviceDetailDtos.add(serviceDetailDto);

        }

        return serviceDetailDtos;
    }

}
